package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.GenealogyPerson;
import com.example.demo.service.GenealogyPersonService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/genealogyPerson")
public class GenealogyPersonController {

    @Resource
    private GenealogyPersonService genealogyPersonService;

    // 新增人员
    @PostMapping("/add")
    public Result add(@RequestBody GenealogyPerson person) {
        genealogyPersonService.add(person);
        return Result.success();
    }

    // 更新人员信息
    @PutMapping("/update")
    public Result update(@RequestBody GenealogyPerson person) {
        genealogyPersonService.update(person);
        return Result.success();
    }

    // 删除人员
    @DeleteMapping("/deleteById/{personId}")
    public Result deleteById(@PathVariable Long personId) {
        genealogyPersonService.deleteById(personId);
        return Result.success();
    }

    // 查询单个人员
    @GetMapping("/selectById/{personId}")
    public Result selectById(@PathVariable Long personId) {
        GenealogyPerson person = genealogyPersonService.selectById(personId);
        return Result.success(person);
    }

    // 查询所有人员
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<GenealogyPerson> list = genealogyPersonService.selectAll();
        return Result.success(list);
    }

    // 根据家谱ID查询人员
    @GetMapping("/selectByGenealogyId/{genealogyId}")
    public Result selectByGenealogyId(@PathVariable Long genealogyId) {
        List<GenealogyPerson> list = genealogyPersonService.selectByGenealogyId(genealogyId);
        return Result.success(list);
    }

    // 分页查询
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<GenealogyPerson> pageInfo = genealogyPersonService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }
}